package cyx.web.utils;

import cyx.web.config.Configer;

import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileTool {

    public static String getFileContent(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        StringBuffer sBuffer = new StringBuffer("");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            sBuffer.append(new String(bytes, 0, len));
        }
        return sBuffer.toString();
    }

    public static String getFileContent(String fileName) throws IOException {
        return getFileContent(new File(Configer.getResource() + fileName));
    }

    public static boolean copyFile(String fromFilePath, String toFilePath) throws IOException {
        File fromFile = new File(fromFilePath);
        if (!fromFile.exists()) {
            throw new FileNotFoundException("源文件不存在");
        }
        File toFile = FileTool.createFile(toFilePath);
        if (toFile == null) {
            throw new FileNotFoundException("文件创建失败");
        }
        FileInputStream ins = new FileInputStream(fromFile);
        FileOutputStream out = new FileOutputStream(toFile);
        byte[] b = new byte[1024];
        int len;
        while ((len = ins.read(b)) != -1) {
            out.write(b, 0, len);
        }
        ins.close();
        out.close();
        return true;
    }

    public static File createDir(String dirPath) {
        File file = new File(dirPath);
        if (file.exists()) {
            return file;
        }
        if (file.mkdirs()) {
            return file;
        } else {
            return null;
        }
    }

    public static File createFile(String dirPath, String fileName) throws IOException {
        if (createDir(dirPath) != null) {
            File file = new File(dirPath + fileName);
            if (file.exists()) {
                return file;
            }
            if (file.createNewFile()) {
                return file;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static File createFile(String filePath) throws IOException {
        int lastIndexOf = filePath.lastIndexOf("/") + 1;
        String dir = filePath.substring(0, lastIndexOf);
        String fileName = filePath.substring(lastIndexOf, filePath.length());
        return FileTool.createFile(dir, fileName);
    }

    public static String getMD5(File file) {
        FileInputStream fis = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fis.read(buffer)) != -1) {
                md.update(buffer, 0, length);
            }
            BigInteger bigInt = new BigInteger(1, md.digest());
            return bigInt.toString(16);
        } catch (IOException ex) {
            return null;
        } catch (NoSuchAlgorithmException ex) {
            return null;
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {

            }
        }
    }

    public static boolean move(String fromFile, String toDir, String fileName) {
        try {
            File startFile = new File(fromFile);
            File tmpFile = new File(toDir);//获取文件夹路径
            if (!tmpFile.exists()) {//判断文件夹是否创建，没有创建则创建新文件夹
                tmpFile.mkdirs();
            }
            if (startFile.renameTo(new File(toDir + fileName))) {
                System.out.println("File is moved successful!");
            } else {
                System.out.println("File is failed to move!");
            }
        } catch (Exception e) {

        }
        return false;
    }

}
